package com.yonyou.jrmob.contacts.schedule.dbhelper;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yonyou.jrmob.contacts.schedule.vo.ContactsVO;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 描述: 数据库管理者
 * 
 * @下午4:11:51
 * @author zhangg
 */
public class DBManager {
	private DBHelper helper;
	private SQLiteDatabase db;

	public DBManager(Context context) {
		helper = new DBHelper(context);
		// 因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0,
		// mFactory);
		// 所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
		setDb(helper.getWritableDatabase());
	}



	

	public void insert() {
		String sql = "insert into tableRange(bp_value,pk_custprice,up_limit,pk_custprice_b,down_limit,type) values ('4.0','1068AA10000000003NY8','500.0','1068AA10000000003NYC','300.0000','1')";
		db.execSQL(sql);
	}
	public void insertRecentlyContact(String user_name,String user_mobilephone,String user_telephone,String user_email,String cuserid) {
		String sql = "insert into recentlycontacts(user_name,user_mobilephone,user_telephone,user_email,cuserid) values ('"+user_name+"','"+user_mobilephone+"','"+user_telephone+"','"+user_email+"','"+cuserid+"')";
		getDb().execSQL(sql);
	}
	public void querychongfu(String user_name,String user_mobilephone,String user_telephone,String user_email,String cuserid) {
		//String flag = "NO";
		Cursor c = getDb()
				.rawQuery("select * from recentlycontacts where cuserid="+"'"+cuserid+"'", null);
		c.getCount();
		if (c.getCount() != 0) {
			//flag = "YES";
			String sql = "delete from " + DBHelper.recentlyContacts + " where cuserid = '"+ cuserid +"'";
			getDb().execSQL(sql);
			insertRecentlyContact(user_name,user_mobilephone,user_telephone,user_email,cuserid);
		}else{
			insertRecentlyContact(user_name,user_mobilephone,user_telephone,user_email,cuserid);
		}
		//return flag;
	}
		/**
		 * query all , return cursor
		 * 
		 * @return Cursor
		 */
		public List<ContactsVO> queryRecentlyContacts() {

			// 创建表(如果不存在的话)
			String sql = "create table if not exists " + DBHelper.recentlyContacts
					+ "(_id integer primary key autoincrement,"
					+ "user_name varchar(50) " + ", user_mobilephone varchar(20)"
					+ ", user_telephone varchar(20) "+ ", user_email varchar(20)"
					+ ", cuserid varchar(20));";
			getDb().execSQL(sql);

			List<ContactsVO> recentlyctList = new ArrayList<ContactsVO>();
			Cursor c = getDb()
					.rawQuery("SELECT * FROM " + DBHelper.recentlyContacts + " ORDER BY _id DESC LIMIT 0,5", null);
			while (c.moveToNext()) {
				ContactsVO contactsVO = new ContactsVO();
				contactsVO.setContactName(c.getString(c
						.getColumnIndex("user_name")));
				contactsVO.setVdef(c.getString(c
						.getColumnIndex("user_mobilephone")));
				contactsVO.setContactTel(c.getString(c
						.getColumnIndex("user_telephone")));
				contactsVO.setUser_email(c.getString(c
						.getColumnIndex("user_email")));
				contactsVO.setCuserId(c.getString(c
						.getColumnIndex("cuserid")));
				recentlyctList.add(contactsVO);

			}
			return recentlyctList;
		}
	public List<ContactsVO> deleteRecentlyContact(String cuserid){
		String sql = "delete from " + DBHelper.recentlyContacts + " where cuserid = '"+ cuserid +"'";
		getDb().execSQL(sql);
		return queryRecentlyContacts();
	}


	/**
	 * close database
	 */
	public void closeDB() {
		getDb().close();
	}

	public SQLiteDatabase getDb() {
		return db;
	}

	public void setDb(SQLiteDatabase db) {
		this.db = db;
	}
}